package org.oep.jmingle;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import org.oep.game.Actor;

public class Minglet extends Actor {
	private String id;
	private String owner;
	private String name;
	private long lastSeen;
	
	// TODO: Make these animations
	public static final int SMILING = Actor.CUSTOM_STATE;
	public static final int ECSTATIC = Actor.CUSTOM_STATE + 1;
	public static final int BORED = Actor.CUSTOM_STATE + 2;
	
	
	// The constructor is JMingle-specific code
	public Minglet(String hash, String owner) {
		this(hash, owner, -1);
	}
	
	public Minglet(String hash, String owner, long time) {
		this.id = hash;
		this.owner = owner;
		this.name = Utils.makeName( Utils.hexToDecimal(hash) );
		lastSeen = time;
	}
	
	public void addAnimation(int state, String spriteSheet, int frameX, int frameY) {
		Image im;
		try {
			im = Image.createImage(spriteSheet);
			im = Utils.customizeMinglet(im, id);
		} catch(IOException e) {
			im = Image.createImage(frameX, frameY);
			Graphics g = im.getGraphics();
			g.setColor(0xFF0000);
			g.drawLine(0, 0, frameX, frameY);
			g.drawLine(frameX, 0, 0, frameY);
		}
		Sprite s = new Sprite(im, frameX, frameY);
		Sprite r = new Sprite(im, frameX, frameY);
		r.setTransform(Sprite.TRANS_MIRROR);
		
		putSprite(state, s);
		
		// Decide if we need to put the mirrored sprite
		if(state == Actor.WALK_LEFT && getSprite(Actor.WALK_RIGHT) == null) {
			putSprite(Actor.WALK_RIGHT, r);
		}
		else if(state == Actor.WALK_RIGHT && getSprite(Actor.WALK_LEFT) == null) {
			putSprite(Actor.WALK_LEFT, r);
		}
	}
	
	public String getName() { return name; }
	public String getOwner() { return owner; }
	public String getAddress() { return id; }
	public long getLastSeen() { return lastSeen; }
	
	
	public void setLastSeen(long time) {
		lastSeen = time;
	}
	
	public String toString() {
		return name;
	}

}
